<?php

require_once 'WSL/Dispatcher/Dispatcher.php';
require_once 'WSL/Dispatcher/Router.php';
require_once 'WSL/Config/Config.php';
require_once 'PHPUnit/Framework/TestCase.php';

class WSL_Dispatcher_Router_Test extends PHPUnit_Framework_TestCase
{
	/**
	 * @var WSL_Dispatcher_Router
	 */
	private $_router;
	
    public function setUp()
    {
    	$array = array('index' =>
    	                   array('route'      => '/',
                                 'controller' => 'Controller.Index',
                                'action'     => 'index'),
                       'simple' =>
    	                   array('route'      => '/example',
                                 'controller' => 'Controller.Example',
    	                         'action'     => 'example'),
                       'simple-multiple' => 
    	                   array('route'   => '/example/mul/tiple',
                                 'controller' => 'Controller.Example',
    	                         'action'     => 'exampleMulti'),
                       'variable' => 
    	                   array('route'          => '/:name',
                                 'controller'     => 'Controller.Example',
    	                         'action'         => 'exampleVar',
    	                         'requirement.name' => '[a-z]+'),
                       'variableComplex' => 
    	                   array('route' => '/example/:year/:month/:day/:name-:name2',
                                 'controller'        => 'Controller.Example',
    	                         'action'            => 'exampleVarComplex',
    	                         'requirement.year'  => '\d{4}',
    	                         'requirement.month' => '\d{2}',
    	                         'requirement.day'   => '\d{2}',
    	                         'requirement.name'  => '[a-z]+',
    	                         'requirement.name2' => '[A-Z0-9]+')
    	);

    	$file = $this->createFile('routes.ini', $array);

    	$config = new WSL_Config($file);

    	$this->_router = new WSL_Dispatcher_Router($config);
    }
    
    public function createFile($fileName, $array)
    {
        touch($fileName);
    	$file = fopen($fileName, 'w+');
    	
    	foreach ($array as $key => $items) {
    		fwrite($file, '['.$key.']'."\n\r");
    		foreach ($items as $key => $value) {
    			fwrite($file, $key. '="' .$value ."\"\n\r");
    		}
    	}
    	
    	return $fileName;
    }
    
    public function tearDown()
    {
    	unlink('routes.ini');
        unset($this->_router);
    }

    public function testGetRoot()
    {
    	/**
    	 * toParams
    	 */
    	$route = $this->_router->toParams('/');
    	
    	$this->assertEquals('/', $route['route']);
    	
    	$this->assertEquals('index', $route['action']);
    	
    	$this->assertEquals('Controller.Index', $route['controller']);
    	
    	/**
    	 * toUrl
    	 */
    	$url = $this->_router->toUrl(array('controller' => 'Controller.Index',
    	                                   'action' => 'index'));
    	$this->assertEquals('/', $url);
    }

    public function testGetSimple()
    {
    	/**
    	 * toParams
    	 */
    	$route = $this->_router->toParams('/example');
    	
    	$this->assertEquals('/example', $route['route']);
    	
    	$this->assertEquals('example', $route['action']);
    	
    	$this->assertEquals('Controller.Example', $route['controller']);
    	
    	/**
    	 * toUrl
    	 */
    	$url = $this->_router->toUrl(array('controller' => 'Controller.Example',
    	                                   'action' => 'example'));
    	$this->assertEquals('/example', $url);
    }

    public function testGetSimpleMultiple()
    {
    	/**
    	 * toParams
    	 */
    	$route = $this->_router->toParams('/example/mul/tiple');

    	$this->assertEquals('/example/mul/tiple', $route['route']);
    	
    	$this->assertEquals('exampleMulti', $route['action']);
    	
    	$this->assertEquals('Controller.Example', $route['controller']);
    	
    	/**
    	 * toUrl
    	 */
    	$url = $this->_router->toUrl(array('controller' => 'Controller.Example',
    	                                   'action' => 'exampleMulti'));
    	$this->assertEquals('/example/mul/tiple', $url);
    }
    
    public function testGetVar()
    {
    	/**
    	 * toParams
    	 */
    	$route = $this->_router->toParams('/thisisavar');

    	$this->assertEquals('/thisisavar', $route['route']);
    	
    	$this->assertEquals('exampleVar', $route['action']);
    	
    	$this->assertEquals('Controller.Example', $route['controller']);
    	
    	/**
    	 * toUrl
    	 */
    	$url = $this->_router->toUrl(array('controller' => 'Controller.Example',
    	                                   'action' => 'exampleVar',
    	                                   'name' => 'test'));
    	$this->assertEquals('/test', $url);
    }
    
    public function testGetVarComplex()
    {
    	/**
    	 * toParams
    	 */
    	$route = $this->_router->toParams('/example/1970/01/01/name-NAME2');

    	$this->assertEquals('/example/1970/01/01/name-NAME2', $route['route']);

    	$this->assertEquals('exampleVarComplex', $route['action']);
    	
    	$this->assertEquals('Controller.Example', $route['controller']);
    	
    	/**
    	 * toUrl
    	 */
    	$url = $this->_router->toUrl(array('controller' => 'Controller.Example',
    	                                   'action'     => 'exampleVarComplex',
    	                                   'year'       => '1970',
    	                                   'month'      => '01',
    	                                   'day'        => '01',
    	                                   'name'       => 'test',
    	                                   'name2'      => 'TEST2'));

    	$this->assertEquals('/example/1970/01/01/test-test2', $url);
    }
    
    
    public function testPageNotFound()
    {
    	$msgException = 'Page not found';
        $this->setExpectedException('Exception', $msgException);
        
    	$route = $this->_router->toParams('/pagenotfound404');
    }
}
?>